package com.tapNhold.android.tipcalcu;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TableRow;

public class TipCalcuComplete {
	private TipCalcuActivity tipCalcu;
	
	private EditText editTextSubtotal = null;
	private EditText editTextTaxAmt = null;
	private EditText editTextTax = null;
	private EditText editTextTipAmt = null;
	private EditText editTextTip = null;
	private EditText editTextTotal = null;
	private EditText editTextDividedby = null;
	private EditText editTextEachpays = null;
	private EditText editTextCouplepays = null;
	
	public TipCalcuComplete(Context context) {
		tipCalcu = (TipCalcuActivity) context;
		editTextSubtotal = (EditText) tipCalcu.findViewById(R.id.editTextSubtotal);
	    
	    // As soft keyboard does not send out key events as expected, use TextWatcher instead. 
	    editTextSubtotal.addTextChangedListener(new TextWatcher()
	    {
		    	@Override
				public void afterTextChanged(Editable arg0) {
		    		// TODO Auto-generated method stub
		    		calculate();		
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub
					
				}
	    });
	    
	    editTextTaxAmt = (EditText) tipCalcu.findViewById(R.id.editTextTaxAmt);
	    editTextTax = (EditText) tipCalcu.findViewById(R.id.editTextTax);
	    
	    editTextTipAmt = (EditText) tipCalcu.findViewById(R.id.editTextTipAmt);
	    editTextTip = (EditText) tipCalcu.findViewById(R.id.editTextTip);
	    
	    //--------------------------------------------------------------------
	    editTextTotal = (EditText) tipCalcu.findViewById(R.id.editTextTotal);
	    editTextDividedby = (EditText) tipCalcu.findViewById(R.id.editTextDividedby);
	    //-------------------------------------------------------------------- 
	    editTextEachpays = (EditText) tipCalcu.findViewById(R.id.editTextEachpays);
	    editTextCouplepays = (EditText) tipCalcu.findViewById(R.id.editTextCouplepays);
	}

	public void calculate() {
		double subtotal = TipCalcuHelper.ParseDouble(editTextSubtotal.getText().toString());		
		double taxPercent = TipCalcuHelper.ParseDouble(editTextTax.getText().toString()) / 100;
		double tipPercent = TipCalcuHelper.ParseDouble(editTextTip.getText().toString()) / 100;
		double dividedby = TipCalcuHelper.ParseDouble(editTextDividedby.getText().toString());
	
		double taxAmt = subtotal * taxPercent;
		double tipAmt = 0;
		double total = 0.0;
		double eachpays = 0.0;
		
		SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(tipCalcu);
		boolean bIncludeTax = myPreference.getBoolean("checkboxIncludeTax", false);
		boolean bShowCouplepays = myPreference.getBoolean("checkboxShowCouplepays", false);
		
		if (dividedby < 1)
			dividedby = 1;
	
		if (bIncludeTax)
			tipAmt = (subtotal+taxAmt) * tipPercent;
		else
			tipAmt = subtotal*tipPercent;
		
		total = subtotal + taxAmt + tipAmt;
		eachpays = total / dividedby;
		
		editTextTaxAmt.setText(TipCalcuActivity.currencyFormat.format(taxAmt));
		editTextTipAmt.setText(TipCalcuActivity.currencyFormat.format(tipAmt));
		editTextTotal.setText(TipCalcuActivity.currencyFormat.format(total));
		editTextEachpays.setText(TipCalcuActivity.currencyFormat.format(eachpays));
		
		// If only one person is paying, no need to display how much a couple should pay
		TableRow trCouplepays = (TableRow) tipCalcu.findViewById(R.id.TableRowCouplepays);
		if (dividedby <= 1) {
			editTextDividedby.setText(editTextDividedby.getHint());
			
			if (bShowCouplepays)
				trCouplepays.setVisibility(View.INVISIBLE);
			else
				trCouplepays.setVisibility(View.GONE);
			
			editTextCouplepays.setText(editTextCouplepays.getHint());
		}
		else
		{
			if (bShowCouplepays)
				trCouplepays.setVisibility(View.VISIBLE);
			
			editTextCouplepays.setText(TipCalcuActivity.currencyFormat.format(eachpays * 2));
		}
	}
	
	public void calculate2(int id) {
		
		if (id != R.id.imageButtonEachpaysAdd &&
			id != R.id.imageButtonEachpaysMinus)
			return;
		
		double subtotal = TipCalcuHelper.ParseDouble(editTextSubtotal.getText().toString());
		if (subtotal <= 0)
			return;
		
		double taxAmt = TipCalcuHelper.ParseDouble(editTextTaxAmt.getText().toString());
		double eachpays = TipCalcuHelper.ParseDouble(editTextEachpays.getText().toString()); 
		double dividedby = TipCalcuHelper.ParseDouble(editTextDividedby.getText().toString());
		
		SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(tipCalcu);
		boolean bIncludeTax = myPreference.getBoolean("checkboxIncludeTax", false);
		boolean bShowCouplepays = myPreference.getBoolean("checkboxShowCouplepays", false);
		
		if (eachpays % 0.5 == 0)
		{
			if (id == R.id.imageButtonEachpaysAdd)
				eachpays += 0.5;
			else
				eachpays -= 0.5;
		}
		else if (eachpays % 1 < 0.5)
		{
			if (id == R.id.imageButtonEachpaysAdd)
				eachpays = Math.ceil(eachpays) - 0.5;
			else
				eachpays = Math.floor(eachpays);
		}
		else
		{
			if (id == R.id.imageButtonEachpaysAdd)
				eachpays = Math.ceil(eachpays);
			else
				eachpays = Math.floor(eachpays) + 0.5;
		}
		
		if (eachpays < 0)
			eachpays = 0;		
			 
		double total = eachpays * dividedby;
		double tipAmt = total - subtotal - taxAmt;
		double tipPercent = 0;
		
		if (tipAmt < 0)
		{
			tipAmt = 0;
			tipPercent = 0;
			total = subtotal + taxAmt;
			eachpays = total / dividedby;
		}
		
		if (bIncludeTax)
			tipPercent = tipAmt / (subtotal+taxAmt);
		else
			tipPercent = tipAmt / subtotal;
		
		editTextEachpays.setText(TipCalcuActivity.currencyFormat.format(eachpays));
		
		if (dividedby <=1) {
			editTextDividedby.setText(editTextDividedby.getHint());
		}
		
		editTextTipAmt.setText(TipCalcuActivity.currencyFormat.format(tipAmt));
		editTextTip.setText(TipCalcuActivity.currencyFormat.format(tipPercent * 100));
		editTextTotal.setText(TipCalcuActivity.currencyFormat.format(total));
		
		// If only one person is paying, no need to display how much a couple should pay
		TableRow trCouplepays = (TableRow) tipCalcu.findViewById(R.id.TableRowCouplepays);
		if (dividedby <= 1)
		{
			if (bShowCouplepays)
				trCouplepays.setVisibility(View.INVISIBLE);
			else
				trCouplepays.setVisibility(View.GONE);
			
			editTextCouplepays.setText(editTextCouplepays.getHint());
		}
		else
		{
			if (bShowCouplepays)
				trCouplepays.setVisibility(View.VISIBLE);
			
			editTextCouplepays.setText(TipCalcuActivity.currencyFormat.format(eachpays * 2));
		}
	}
	
	public void clear() {
		SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(tipCalcu);		
		editTextTax.setText(myPreference.getString("TaxPercentage", (String) editTextTax.getHint()));
		editTextTaxAmt.setText(editTextTaxAmt.getHint());
		editTextTip.setText(myPreference.getString("TipPercentage", (String) editTextTip.getHint()));
		editTextTipAmt.setText(editTextTipAmt.getHint());
		editTextTotal.setText(editTextTotal.getHint());
		editTextDividedby.setText(editTextDividedby.getHint());
		editTextEachpays.setText(editTextEachpays.getHint());
		editTextCouplepays.setText(editTextCouplepays.getHint());
		
		editTextSubtotal.setText(editTextSubtotal.getHint());
		editTextSubtotal.setSelected(true);
		editTextSubtotal.selectAll();
	}
}
